package com.yaozheng.vocationaltraining.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.CarCategoryListAdapter;
import com.yaozheng.vocationaltraining.adapter.CarTypeDetailListAdapter;
import com.yaozheng.vocationaltraining.bean.BaoXianBean;
import com.yaozheng.vocationaltraining.bean.CarCategoryBean;
import com.yaozheng.vocationaltraining.bean.CarDetailBean;
import com.yaozheng.vocationaltraining.bean.CarTypeBean;
import com.yaozheng.vocationaltraining.bean.CarTypeDetailBean;
import com.yaozheng.vocationaltraining.bean.DownPaymentBean;
import com.yaozheng.vocationaltraining.bean.HundredQuestionsEncyclopediaItemBean;
import com.yaozheng.vocationaltraining.bean.RepaymentReriodBean;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.fragment.PriceFragment;
import com.yaozheng.vocationaltraining.iview.ICarTypeDetailView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.BuyCalculatorService;
import com.yaozheng.vocationaltraining.service.CarTypeDetailService;
import com.yaozheng.vocationaltraining.service.impl.BuyCalculatorServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.CarTypeDetailServiceImpl;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by moon on 2017/11/14.
 */

@EFragment(R.layout.activity_car_type_detail)
public class CarTypeDetailActivity extends BaseFragment
            implements ICarTypeDetailView {

    @ViewById
    ListView listView;
    @ViewById
    TextView headTitleText;

    LayoutInflater inflater;

    @Bean(CarTypeDetailServiceImpl.class)
    CarTypeDetailService carTypeDetailService;

    CarTypeDetailListAdapter carTypeDetailListAdapter;
    //List<CarTypeDetailBean> carTypeDetailData = new ArrayList<>();

    List<CarTypeBean> carTypeBeanList = new ArrayList<>();

    public int getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

    int categoryId;

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    String categoryName;

    ArrayList<DownPaymentBean> downPaymentBeanList = new ArrayList<>();
    ArrayList<RepaymentReriodBean> rePaymentPeriodBeanList = new ArrayList<>();

    @AfterViews
    public void initView() {
        headTitleText.setText("购车计算器");
        inflater = this.getActivity().getLayoutInflater();

        carTypeDetailService.init(this);

//        categoryId = this.getActivity().getIntentInt("categoryId");

        carTypeDetailListAdapter = new CarTypeDetailListAdapter(this.getActivity(),
                carTypeBeanList);
        listView.setAdapter(carTypeDetailListAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Intent intent = new Intent();
//                intent.setClassName(CarTypeDetailActivity.this,
//                        Constants.CAR_TYPE_DETAIL_ACTIVITY_CLASS_NAME);
//                intent.putExtra("categoryId",carCategoryData.get(i).getId());
//                CarTypeDetailActivity.this.startActivity(intent);
            }
        });


        //onRefresh();

        showOperatingProgressDialog("数据正在加载中...");
        carTypeDetailService.repaymentPeriod();//查询还款年限


    }

    private void onRefresh() {
        carTypeDetailService.list(categoryId);
    }

    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        try {
            cancelOperatingProgressDialog();
            JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data","types");
            if (dataList != null) {
                carTypeBeanList.clear();

                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject jsonObject2 = dataList.getJSONObject(i);

                    CarTypeBean typeBean = new CarTypeBean();
                    typeBean.setId(jsonObject2.getInt("id"));
                    typeBean.setCate(jsonObject2.getInt("cate"));
                    typeBean.setIntroduce(jsonObject2.getString("introduce"));
                    typeBean.setName(jsonObject2.getString("name"));

                    ArrayList<CarDetailBean> carDetailList = new ArrayList<>();
                    JSONArray carsArray = TypeUtils.getJsonArray(jsonObject2,"cars");
                    for (int j = 0; j < carsArray.size(); j++) {
                        JSONObject carJsonObject = carsArray.getJSONObject(j);
                        CarDetailBean detail =new CarDetailBean();
                        detail.setId(carJsonObject.getInt("id"));
                        detail.setCate(carJsonObject.getInt("cate"));
                        detail.setType(carJsonObject.getInt("type"));
                        detail.setName(carJsonObject.getString("name"));
                        detail.setPrice((float) carJsonObject.getDouble("price"));
                        detail.setGouzhishui(carJsonObject.getInt("gouzhishui"));
                        detail.setShangpaifei(carJsonObject.getInt("shangpaifei"));
                        detail.setChechuanshiyongshui(carJsonObject.getInt("chechuanshiyongshui"));
                        detail.setJiaoqiangxian(carJsonObject.getInt("jiaoqiangxian"));
                        detail.setXiaofeishui(carJsonObject.getInt("xiaofeishui"));
                        detail.setXiaofeishuiName(carJsonObject.getString("xiaofeishuiName"));

                        detail.setCheliangshunshixian(carJsonObject.getInt("cheliangshunshixian"));


                        JSONArray array = carJsonObject.getJSONArray("disanzhezerenxian");
                        ArrayList<BaoXianBean> disanzhezerenxian = new ArrayList<>();
                        for (int k = 0; k < array.size(); k++) {
                            JSONArray baoxianArray = array.getJSONArray(k);
                            BaoXianBean baoXianBean = new BaoXianBean();
                            baoXianBean.setKey(baoxianArray.getString(0));
                            baoXianBean.setValue(Integer.parseInt(baoxianArray.getString(1)));
                            baoXianBean.setDefaultStatus(baoxianArray.getString(2));

                            if(baoXianBean.getDefaultStatus().equals("1")){//表示默认值
                                detail.setWuguozerenxian((int)(baoXianBean.getValue() * 20 / 100));

                                detail.setBujimianpeiteyuexian((int)((detail.getCheliangshunshixian()+baoXianBean.getValue())
                                        *20 / 100)
                                );
                            }



                            disanzhezerenxian.add(baoXianBean);
                        }
                        detail.setDisanzhezerenxian(disanzhezerenxian);


                        detail.setQuanchedaoqiangxian(carJsonObject.getInt("quanchedaoqiangxian"));


                        JSONArray boliarray = carJsonObject.getJSONArray("bolidanduposuixian");
                        ArrayList<BaoXianBean> bolidanduposuixian = new ArrayList<>();
                        for (int k = 0; k < boliarray.size(); k++) {
                            JSONArray baoxianArray = boliarray.getJSONArray(k);
                            BaoXianBean baoXianBean = new BaoXianBean();
                            baoXianBean.setKey(baoxianArray.getString(0));
                            baoXianBean.setValue(Float.parseFloat(baoxianArray.getString(1)));
                            baoXianBean.setDefaultStatus(baoxianArray.getString(2));
                            bolidanduposuixian.add(baoXianBean);
                        }
                        detail.setBolidanduposuixian(bolidanduposuixian);
                        detail.setZiransunshixian(carJsonObject.getInt("ziransunshixian"));
                        detail.setCheshangrenyuanzerenxian(carJsonObject.getInt("cheshangrenyuanzerenxian"));


                        JSONArray huahenarray = carJsonObject.getJSONArray("cheshenhuahenxian");
                        ArrayList<BaoXianBean> huahenxian = new ArrayList<>();
                        for (int k = 0; k < huahenarray.size(); k++) {
                            JSONArray baoxianArray = huahenarray.getJSONArray(k);
                            BaoXianBean baoXianBean = new BaoXianBean();
                            baoXianBean.setKey(baoxianArray.getString(0));
                            baoXianBean.setValue(Integer.parseInt(baoxianArray.getString(1)));
                            baoXianBean.setDefaultStatus(baoxianArray.getString(2));
                            huahenxian.add(baoXianBean);
                        }
                        detail.setCheshenhuahenxian(huahenxian);

                        detail.setSheshuixian(carJsonObject.getInt("sheshuixian"));
                        detail.setIntroduce(carJsonObject.getString("introduce"));
                        detail.setSeatNumber(carJsonObject.getInt("SeatNumber"));
                        detail.setSeatName(carJsonObject.getString("seatName"));
                        detail.setDisplacement(carJsonObject.getString("displacement"));
                        detail.setDisplacementName(carJsonObject.getString("displacementName"));
                        detail.setInlet(carJsonObject.getString("inlet"));
                        detail.setHorsepower(carJsonObject.getInt("horsepower"));
                        detail.setImage(carJsonObject.getString("image"));
                        detail.setSort(carJsonObject.getInt("sort"));
                        detail.setStatus(carJsonObject.getInt("status"));


                        carDetailList.add(detail);
                    }
                    typeBean.setCars(carDetailList);

                    carTypeBeanList.add(typeBean);

                }

                carTypeDetailListAdapter.setData(carTypeBeanList,downPaymentBeanList,rePaymentPeriodBeanList,categoryName);
                carTypeDetailListAdapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
        }
    }

    @UiThread
    @Override
    public void responseError(String error) {
        Toast.makeText(this.getActivity(),error,Toast.LENGTH_SHORT).show();
    }

    @UiThread
    @Override
    public void repaymentPeriodSuccess(JSONObject jsonObject) {
        JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data","rates");
        if (dataList != null) {
            rePaymentPeriodBeanList.clear();
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject jsonObject2 = dataList.getJSONObject(i);
                RepaymentReriodBean bean  = new RepaymentReriodBean();
                bean.setId(jsonObject2.getInt("id"));
                bean.setName(jsonObject2.getString("name"));
                bean.setDefaultValue(jsonObject2.getInt("defaultValue"));
                bean.setDefault(jsonObject2.getBoolean("IsDefault"));
                bean.setInterestRate((float) jsonObject2.getDouble("InterestRate"));
                rePaymentPeriodBeanList.add(bean);
            }
            carTypeDetailService.downPaymentRatios();//查询首付比例
        }
    }

    @UiThread
    @Override
    public void repaymentPeriodError(String error) {
        Toast.makeText(this.getActivity(),error,Toast.LENGTH_SHORT).show();
    }

    @UiThread
    @Override
    public void downPaymentSuccess(JSONObject jsonObject) {

        JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data","ratios");
        if (dataList != null) {
            downPaymentBeanList.clear();
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject jsonObject2 = dataList.getJSONObject(i);
                DownPaymentBean bean  = new DownPaymentBean();
                bean.setId(jsonObject2.getInt("id"));
                bean.setDownPayment((float) jsonObject2.getDouble("DownPayment"));
                bean.setDefaultValue(jsonObject2.getInt("defaultValue"));
                bean.setDefault(jsonObject2.getBoolean("IsDefault"));
                downPaymentBeanList.add(bean);
            }
            onRefresh();//查询具体车辆详情
        }

    }

    @UiThread
    @Override
    public void downPaymentError(String error) {
        Toast.makeText(this.getActivity(),error,Toast.LENGTH_SHORT).show();
    }
}
